package org.dbrd.util;

public class RandomArray<T> {

	private T[] array;

	private RandomWrapper random = new RandomWrapper();

	private int index;
	
	public RandomArray(T[] array) {
		super();
		this.array = array;
	}
	
	public T next() {
		index = (int) (random.nextDouble() * array.length);
		for (int i = index; i < array.length; i++) {
			T t = array[i];
			if (t != null) {
//				array[i] = null;
				return t;
			}
		}
		for (int i = 0; i < index; i++) {
			T t = array[i];
			if (t != null) {
//				array[i] = null;
				return t;
			}
		}
		throw new RuntimeException("no more elements");
	}
	
	public void remove() {
		array[index] = null;
	}
	
}
